﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Course;

public partial class sysadmin_VirtualManager_EditVirtual : PowerCreator.LMS.Web.Base.BasePage
{
    VirtualDirectory opVirtual = new VirtualDirectory();

    protected void Page_Load(object sender, EventArgs e)
    {       
        if (!IsPostBack)
        {
            InitPage();

            string virtualid = Request.QueryString["VirtualID"].ToString();
            ViewState["VirtualID"] = virtualid;

            VOTVirtualDirectory vo = opVirtual.LoadVirtual(int.Parse(virtualid));
            txt_VirFtpName.Text = vo.VirFtpName;
            txt_VirFtpPassword.Text = vo.VirFtpPassword;
            txt_VirFtpPort.Text = vo.VirFtpPort.ToString();
            txt_VirIisName.Text = vo.VirIisName;
            txt_VirIp.Text = vo.VirIp;
            txt_VirMmsName.Text = vo.VirMmsName;
            txt_VirPath.Text = vo.VirPath;
        }
    }
    protected override void InitPage()
    {
        lbl_EditVirtual_InfoTitle.Text = GetLanageValueByKey("lbl_EditVirtual_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_Information.Text = GetLanageValueByKey("lbl_EditVirtual_Information", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_VirIisName.Text = GetLanageValueByKey("lbl_EditVirtual_VirIisName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_VirPath.Text = GetLanageValueByKey("lbl_EditVirtual_VirPath", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_VirIp.Text = GetLanageValueByKey("lbl_EditVirtual_VirIp", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_WMServices.Text = GetLanageValueByKey("lbl_EditVirtual_WMServices", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_VirMmsName.Text = GetLanageValueByKey("lbl_EditVirtual_VirMmsName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_FTP.Text = GetLanageValueByKey("lbl_EditVirtual_FTP", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_VirFtpName.Text = GetLanageValueByKey("lbl_EditVirtual_VirFtpName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_VirFtpPassword.Text = GetLanageValueByKey("lbl_EditVirtual_VirFtpPassword", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditVirtual_VirFtpPort.Text = GetLanageValueByKey("lbl_EditVirtual_VirFtpPort", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_title.Text = GetLanageValueByKey("lbl_EditVirtual_Description_title", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_1.Text = GetLanageValueByKey("lbl_EditVirtual_Description_1", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_2.Text = GetLanageValueByKey("lbl_EditVirtual_Description_2", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_3.Text = GetLanageValueByKey("lbl_EditVirtual_Description_3", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_4.Text = GetLanageValueByKey("lbl_EditVirtual_Description_4", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Save.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    private bool CheckPageData()
    {
        string search = string.Format(" VirIisName='{0}' and VirId <> {1}", txt_VirIisName.Text, ViewState["VirtualID"].ToString());
        if (opVirtual.QueryVirtualCount(search) != 0)
        {
            lbl_EditVirtual_msg_viriisname.Text = GetLanageValueByKey("lbl_EditVirtual_msg_viriisname", PowerCreator.LMS.Language.LanagetItemType.MSG);
            return false;
        }
        
        
            //if (!IPUtils.IsIP(txt_VirIp.Text))
            //{
            //    lbl_EditVirtual_msg_ip.Text = GetLanageValueByKey("lbl_EditVirtual_msg_ip", PowerCreator.LMS.Language.LanagetItemType.MSG);
            //    return false;
            //}
            if (!string.IsNullOrEmpty(txt_VirFtpPort.Text))
            {
                if (!IntUtils.IsNumber(txt_VirFtpPort.Text))
                {
                    lbl_EditVirtual_msg_virftpport.Text = GetLanageValueByKey("lbl_EditVirtual_msg_virftpport", PowerCreator.LMS.Language.LanagetItemType.MSG);
                    return false;
                }
            }

        return true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;

        VOTVirtualDirectory vo = opVirtual.LoadVirtual(int.Parse(ViewState["VirtualID"].ToString()));
        string oldCourseName = vo.VirIisName;
        string oldip = vo.VirIp;
        vo.VirFtpName = txt_VirFtpName.Text;
        vo.VirFtpPassword = txt_VirFtpPassword.Text;
        if (!string.IsNullOrEmpty(txt_VirFtpPort.Text))
        {
            vo.VirFtpPort = int.Parse(txt_VirFtpPort.Text);
        }
        vo.VirIisName = txt_VirIisName.Text;
        vo.VirIp = txt_VirIp.Text;
        vo.VirMmsName = txt_VirMmsName.Text;
        vo.VirPath = txt_VirPath.Text;
        if (oldip != vo.VirIp)
        {
            PowerCreator.LMS.Web.Course.Resource opResource = new Resource();
            opResource.UpdataResourceMMSSate(oldCourseName);
        }

        if (opVirtual.UpdateVirtual(vo))
        {
            string url = "VirtualManager.aspx";
            Script.RedirectURL(url);
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "VirtualManager.aspx";
        Script.RedirectURL(url);
    }
}
